package com.goxueche.lib_core.ui.mvp.presenter;

import android.content.Context;

import androidx.annotation.NonNull;

import com.goxueche.lib_core.CoreApplication;
import com.goxueche.lib_core.ui.mvp.view.IBaseView;
import com.goxueche.lib_core.widgets.Alert;

import java.lang.ref.WeakReference;

public class BasePresenter<V extends IBaseView> implements IBasePresenter {

    /**
     * Activity/Fragment的弱引用
     */
    protected WeakReference<V> viewRef;

    /**
     * 将Activity或Fragment绑定到业务逻辑层Presenter
     *
     * @param view
     */
    public BasePresenter(@NonNull V view) {
        viewRef = new WeakReference<V>(view);
    }

    /**
     * 获取Activity/Fragment
     *
     * @return
     */
    public V getView() {
        return viewRef.get();
    }


    public Context getApplicationContext() {
        return CoreApplication.getInstance().getApplicationContext();
    }

    /**
     * 检查Activity的引用是否存在
     *
     * @return 如果Activity/Fragment的Ref引用容器存在，并且容器中确实存在Activity/Fragment，那么，该操作将会返回true。如果已经被释放，则返回false。
     */
    public boolean isViewAttached() {
        return viewRef != null && viewRef.get() != null;
    }


    public void showToast(String msg) {
        Alert.toast(msg);
    }

    @Override
    public void onDestory() {

    }
}

